
package com.tt.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tt.dao.StrategyDaoImpl;
import com.tt.entity.ListImg;
import com.tt.entity.Page;
import com.tt.entity.Strategy;
import com.tt.entity.User;

@Service
@Transactional
public class StrategyServiceImpl {
	@Resource
	private StrategyDaoImpl strategyDaoImpl;
	//发布动态
	public boolean addStrtegy(Strategy strategy,Integer uid,Set<String> listImgs){
		Set<ListImg> lSet=new HashSet<ListImg>();
		for(String s:listImgs){
			ListImg lImg=new ListImg(s);
			lSet.add(lImg);
		}
		int addStrategy = strategyDaoImpl.addStrategy(strategy,uid,lSet);
		if(addStrategy>0){
			return true;
		}
		return false;
	}
	//删除动态
	public boolean deleteStrategy(int id){
		strategyDaoImpl.deleteStrategy(id);
		return true;
	}
	//查看动态
	public Strategy findStraegyById(int id){
		Strategy strategy = strategyDaoImpl.findStraegyById(id);
		return strategy;
	}
	//获取动态的图片列表
	public List<ListImg> findStrategyListImgById(int id){
		return this.strategyDaoImpl.findStrategyListImgById(id);
	}
	//删除图片列表
	public void deleteListImg(List<ListImg> listImgs){
		this.strategyDaoImpl.deleteListImg(listImgs);
	}
	//修改动态
	public boolean  updateStrategy(Strategy strategy){
		//先判断修改动态的图片列表是否为空，为空则继续使用原来的图片，否则更新为新的图片列表
		if (strategy.getListimg()==null) {
			List<ListImg> set = strategyDaoImpl.findStrategyListImgById(strategy.getId());
			strategy.setListimg(set);
		}
		strategyDaoImpl.updateStrategy(strategy);
		return true;
	}
	//查询所有动态
	public Page<Strategy> findAllStrategy(int pageNum,int pageSize){
		return strategyDaoImpl.findAllStrategy(pageNum,pageSize);
	}
	//查询该用户的所有动态
	public Page<Strategy> findUserAllStrategy(Integer uid,int pageSize,int pageNum){
		return strategyDaoImpl.findUserStrategyById(uid,pageSize,pageNum);
	}
	//查询动态（模糊查询）
	public Page<Strategy> searchStrategy(String search,int pageNum,int pageSize){
		return strategyDaoImpl.searchStrategy(search,pageNum,pageSize);
	}
	//动态按地址分类
	public Page<Strategy> classificationStrategy(int pageNum,int pageSize,String address){
		return strategyDaoImpl.classificationStrategy(pageNum, pageSize, address);
	}
	//根据id查询用户
	public User findUserById(int id){
		return this.strategyDaoImpl.findUserById(id);
	}
}
